package com.hdu.other.tanxin;

public class lc581 {
    class Solution {
        public int findUnsortedSubarray(int[] nums) {
            int right = -1;
            int max = Integer.MIN_VALUE;
            boolean needChange = false;
            for (int i = 0; i < nums.length; i++) {
                if (nums[i] < max)
                {
                    needChange = true;
                    right = i;
                }
                max = Math.max(max, nums[i]);
            }
            int left = - 1;
            max = Integer.MAX_VALUE;
            for (int i = nums.length - 1; i >= 0; i--) {
                if (nums[i] > max)
                {
                    needChange = true;
                    left = i;
                }
                max = Math.min(max, nums[i]);
            }
            return needChange ? right - left + 1 : 0;
        }
    }
}
